<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/7
 * Time: 15:12
 */

namespace app\admin\controller;
use think\Request;

class User extends common
{
    public function userAdd(){
        if(Request::instance()->isPost()){
            $name = input('post.name');
            $pass = input('post.pass');
            $repass = input('post.repass');
            $lv= input('post.lv/d');
            $rebate = input('post.rebate/d');
            $remark = input('post.remark');
            if(trim($name) != '' && trim($pass) != '' && trim($lv) != ''){
                if($pass == $repass){
                    $bool= db('user')->where('name',$name)->find();
                    if($bool){
                        return json_encode(['msg'=>0,'data'=>'用户名被占用!']);
                    }
                    $insert['name'] = $name;
                    $insert['pass'] = md5($pass);
                    $insert['lv'] = $lv;//代理or会员
                    $insert['rebate'] = $rebate;//返点
                    $insert['come_type'] = 1;//来源1后台
                    $insert['login_ip'] = Request::instance()->ip();
                    $insert['remark'] = $remark;//备注
                    $insert['login_time'] = time();//注册时间

                    if(db('user')->insert($insert)){
                        return json_encode(['msg'=>1,'data'=>'添加成功!']);
                    }else{
                        return json_encode(['msg'=>0,'data'=>'添加失败!']);
                    }
                }else{
                    return json_encode(['msg'=>0,'data'=>'两次密码不同!']);
                }
            }else{
                return json_encode(['msg'=>0,'data'=>'数据为空!']);
            }
        }else{
            return $this->fetch();
        }

    }
    public function userList(){
        $user= input('get.user');
        if(trim($user) != ''){
            $userList = db('user')->where('id',$user)->whereOr('name',$user)->paginate(20);
        }else{
            $userList = db('user')->paginate(20);
        }
        //分页
        $page = $userList->render();
        $this->assign('page', $page);
        $userList = $this->guanxi($userList);
        $this->assign('userList',$userList);
        return $this->fetch('userlist');
    }

    public function userEdit(){
        if(Request::instance()->isPost()){
            $id = input('post.id/d');
            $pass = input('post.pass');
            $money_pass = input('post.money_pass');
            $rebate = input('post.rebate');
            $money = input('post.money');
            $state = input('post.state');
            $remark = input('post.remark');
            if($id > 0){
                if(trim($pass) != ''){
                    $data['pass'] = md5($pass);
                }
                if(trim($money_pass) != ''){
                    $data['money_pass'] = $money_pass;
                }
                //判断返点
                //最大点
                $gx =  db('user')->where('id',$id)->value('gx');
                if($gx == 0){
                    $maxfy = 10;//为后台添加默认最大为10%
                }else{
                    $maxfy = db('user')->where('id',$gx)->value('rebate');
                }
                //最小点
                $minfy = db('user')->where('gx',$id)->max('rebate');
                if(!$minfy) {
                    $minfy = 0;
                }
                if($rebate >= $minfy && $rebate <= $maxfy){
                    $data['rebate'] = $rebate;
                }else{
                    return json_encode(['msg'=>0,'data'=>'返点超出范围!']);
                }
                $data['money'] = $money;
                $data['state'] = $state;
                $data['remark'] = $remark;
                $bool = db('user')->where('id',$id)->update($data);
                if($bool){
                    return json_encode(['msg'=>1,'data'=>'更新成功,请刷新页面!']);
                }else{
                    return json_encode(['msg'=>0,'data'=>'更新失败!']);
                }
            }else{
                return json_encode(['msg'=>0,'data'=>'提交数据为空!']);
            }
        }else{
            $id = input('get.id/d');
            $data =  db('user')->where('id',$id)->select();
            //查找用户返点范围
            //判断最大返点
            $gx = $data[0]['gx'];
            if($gx == 0){
                $data[0]['maxfy'] = 10;//为后台添加默认最大为10%
            }else{
                $data[0]['maxfy'] = db('user')->where('id',$gx)->value('rebate');
            }
            //判断最小返点
            $min = db('user')->where('gx',$id)->max('rebate');
            if($min){
                $data[0]['minfy'] =$min;
            }else{
                $data[0]['minfy'] = 0;
            }
            //处理关系显示
            $data = $this->guanxi($data);
            $this->assign('data',$data[0]);
            return $this->fetch();
        }

    }

    //查找下级用户
    public function userSon(){
        $id = input('get.id/d');
        if($id > 0){
            $userList =  $this->xiaji($id,[]);
            $userList = $this->guanxi($userList);
            $this->assign('userList',$userList);
            $this->assign('page', '');//防止分页报错
            return $this->fetch('userlist');
        }else{
            $this->error('参数错误!');
        }

    }

}